<?php   // $Id: fact_bon_orph.php 313 2008-11-01 21:59:40Z pcoustillas $ ?>
<?php   
/*Copyright (C) 2003-2004 Guy Hendrickx
Licensed under the terms of the GNU  General Public License:http://www.opensource.org/licenses/gpl-license.php
File Authors:Guy Hendrickx
Modification : Pierre Coustillas*/

require_once("include/verif.php");
require_once("include/config/common.php");
require_once("include/config/var.php");
require_once("include/language/$lang.php");
require_once("include/utils.php");
require_once("include/headers.php");




$sql_fact = "SELECT date_fact FROM ${tblpref}facture ORDER BY date_fact DESC LIMIT 1";
$result = mysql_query($sql_fact) or die("Erreur SQL !<br/>$sql_fact<br/>" . mysql_error());
if (mysql_num_rows($result))
{ //une facture à été trouvé
		$date_derniere_fact = mysql_result($result,0);
		$date_derniere_fact = sql_to_date($date_derniere_fact); //ne pas déplacer après l'appel à javascript
} else {
	$date_derniere_fact="00/00/0000";
}
?>

<?php // ne pas déplacer avant le calcul de $date_dernier_fact ?>
<script language="javascript" src="javascripts/verifier_form.js" type="text/javascript"></script>
<script type="text/javascript">

	function verif_creer_fact()
	{
		if (verifier('formu','black','date','vide#date_#est_inf_a_date%<?php echo $date_derniere_fact ?>','acompte','est_nombre'))
		{
			document.formu.submit();
		} else {
			alert("<?php aff_lang('Une des valeurs saisie n\'est pas correct.\nMerci de corriger avant de poursuivre.') ?>")
		}

	}
</script>



<table width="760" border="0" class="page" align="center">
	<tr>
		<td class="page" align="center">
		<?php
		require_once("include/head.php");
		?>
		</td>
	</tr>
	<tr>
		<td>
			<center>
			<form name="formu" method="post" action="fact_bon_orph_suite.php" >
			<table>
				<?php
				if ($generique->perm("user_fact") == "n") {
					echo"<h1>$lang_facture_droit</h1>";
					exit;
				}

				$num=isset($_GET['num'])?$_GET['num']:"";
				$client=isset($_GET['client'])?$_GET['client']:"";
				$jour = date("d");
				$mois = date("m");
				$annee = date("Y");

				if ($client != "") //normalement $client est toujours renseigné
				{
					$sql_client = "SELECT nom FROM ${tblpref}client WHERE num_client=$client";
					$result =mysql_query($sql_client) or die("Erreur sql_client : selection nom du client !<br/>$sql_client<br/>" . mysql_error());
					$nom_client = mysql_result($result,0);
				}
				?>


				<caption><?php echo get_lang("Créer une facture pour ") . " $nom_client"; ?> &nbsp;  &nbsp;  &nbsp; <?php bouton_aide("fact_creer_depuis_bon") ?></caption>

				<tr>
					<td class="texte0" id="date">
						<?php echo $lang_facture_date; ?> (<?php echo get_lang("date de la dernière facture") . " : " . $date_derniere_fact . ")" ?>
					</td>
					<td class="texte0">
						<input type="text" name="date" value="<?php   echo"$jour/$mois/$annee" ?>" />
						<a href="#" onclick=" window.open('include/pop.calendrier.php?frm=formu&amp;ch=date','calendrier','width=415,height=160,scrollbars=0').focus();">
							<img src="image/petit_calendrier.gif" alt="calendrier" border="0"/>
						</a>
					</td>
				</tr>
				<tr>
					<td class="texte0"><?php aff_lang("Ajouter un autre bon du même client à cette facture") ?> </td>
					<?php
					//selectionner les bon du meme client pas encore facturé.
					$rqSql = "SELECT  num_bon, tot_htva, tot_tva, DATE_FORMAT(date,'%d/%m/%Y') AS date 
					FROM ${tblpref}bon_comm WHERE client_num ='$client' AND num_bon !='$num' AND num_facture=0 
					ORDER BY ${tblpref}bon_comm.num_bon DESC";
					$result = mysql_query( $rqSql ) or die("Erreur SQL : selection des bons de commandes du client !<br/>$rqSql<br/>" . mysql_error());?>
					
					<td class="texte0">
						<select multiple="multiple" size="12" name="bon_sup[]">
						<?php
							while ( $row = mysql_fetch_array( $result))
							{
								$num_bon = $row["num_bon"];
								$tot = $row["tot_htva"];
								$date = $row["date"];
							?>
						
							<option value="<?php echo $num_bon ?>">
								<?php
									//remplir avec des &nbsp; pour aligner.
									$space_num_bon="";
									for ($i=0;$i<3-strlen($num_bon);$i++)
									{
										$space_num_bon=$space_num_bon . "&nbsp;&nbsp;";
									}
									$space_tot="";
									for ($i=0;$i<9-strlen($tot);$i++)
									{
										$space_tot=$space_tot . "&nbsp;";
									}
								?>
								<?php echo "$lang_num_bon_ab $space_num_bon $num_bon &nbsp; du &nbsp; $date &nbsp; $lang_Pour_mont $space_tot $tot $devise"; ?>
							</option>
							<?php
							}
						?>
						</select>
					</td>
				</tr>

				<tr>
					<td class="texte0" id="acompte">
						<?php aff_lang("Acompte") ?>
					</td>
					<td class="texte0">
						<input type="text" name="acompte"/><?php echo $devise ?>
					</td>
				</tr>

				<tr>
					<td class="texte0">
						<?php echo $lang_ajo_fact ?>
					</td>
					<td class="texte0" colspan="2">
						<textarea name="coment" cols="45" rows="3"></textarea>
					</td>
				</tr>

				<tr>
					<td class="texte0">
						<?php aff_lang("Ajouter une référence à la facture") ?>
					</td>
					<td class="texte0" colspan="2">
						<input type="text" name="reference" size="70" maxlength="80" />
					</td>
				</tr>

				<tr>
					<td class="submit" colspan="2">
						<input type="hidden" name="num" value="<?php echo "$num" ?>" />
						<input type="hidden" name="client" value="<?php echo "$client" ?>" />
						<input type="button" onclick="verif_creer_fact()" value="<?php echo $lang_facture_creer_bouton; ?>" />
					</td>
				</tr>

			</table>
			</form>
			</center>
		</td>
	</tr>
	<tr>
		<td>
			<?php
			require_once("include/bas.php");
 			?>
		</td>
	</tr>
</table>
</body>
</html>


